//
//  OYToolBar.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/12/9.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

enum OYToolBarItemType:Int {
    case picture = 0
    case mention = 1
    case trend = 2
    case emoticon = 3
    case plus = 4
}

class OYToolBar: UIStackView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    
    /// button点击事件的回调闭包
    var closure:((OYToolBarItemType)->())?
    var emoticonButton: UIButton?
    
    
    func setupUI(){
        
        setButton(imageName: "camerabutton", buttonType: .picture)
        setButton(imageName: "mentionbutton", buttonType: .mention)
        setButton(imageName: "trendbutton", buttonType: .trend)
        emoticonButton = setButton(imageName: "emoticonbutton", buttonType: .emoticon)
        setButton(imageName: "add", buttonType: .plus)
        
    }
    
    @discardableResult
    func setButton(imageName:String,buttonType:OYToolBarItemType) -> UIButton {
        // compose_camerabutton_background_highlighted
        let button = UIButton()
        button.setImage(UIImage(named:"compose_\(imageName)_background"), for: .normal)
        button.setImage(UIImage(named:"compose_\(imageName)_background_highlighted"), for: .highlighted)
        
        button.tag = buttonType.rawValue
        button.addTarget(self, action: #selector(dealingWeibo(sender:)), for: .touchUpInside)
        button.sizeToFit()
        
        self.addArrangedSubview(button)
        
        return button
    }
    
    
     // MARK: -button点击事件的处理
    func dealingWeibo(sender:UIButton) {
        
        let type = OYToolBarItemType(rawValue: sender.tag)!
        
        closure?(type)
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
